package cn.dengta.webapp.trade.model;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import cn.dengta.common.model.*;
import cn.dengta.common.util.Common;
import cn.dengta.common.web.Client;
import lombok.Getter;

/**
 * 第三方支付平台（包含充值和提现）
 * <p>
 * 支付平台：1=Google；2=Apple；3=Wxpay；4=GeeWallet；5=YooPay；6=NganPay；7=PayerMax；8=Alipay；9=Meishitong
 */
@Getter
public enum PayPlatform implements ValueProxyImpl<PayPlatform, Integer> {

	Google(1, "GG", true, false, false, Client.APP_ANDROID),
	Apple(2, "AP", true, false, false, Client.APP_IOS),
	Wxpay(3, "WX", true, true, false, Client.WAP, Client.APP_ANDROID, Client.APP_IOS),
	GeeWallet(4, "GW", true, false, false, Client.WAP, Client.APP_ANDROID, Client.APP_IOS),
	/** 实际未使用，之前走的线下，因此 YooPay 实际表示提现线下到账 */
	@Deprecated
	YooPay(5, "YP", false, true, false, Client.APP_ANDROID, Client.APP_IOS),
	/** 仅支持越南盾支付 */
	@Deprecated
	NganPay(6, "NP", true, true, false, Client.PC),
	PayerMax(7, "PM", true, true, true, Client.WAP, Client.APP_ANDROID, Client.APP_IOS),
	Alipay(8, "AL", true, true, true, Client.WAP, Client.APP_ANDROID, Client.APP_IOS),
	Meishitong(9, "MT", false, true, true, Client.WAP),
	Razer(10, "RZ", true, false, false, Client.WAP),
	Payssion(11, "PS", true, false, false, Client.WAP),
	CloudAccount(12, "CA", false, true, true, Client.APP_IOS, Client.APP_ANDROID, Client.WECHAT_MP),
	/** 银联，国内提现类型 */
	UnionPay(13, "CUP", false, true, true, Client.WAP, Client.APP_ANDROID, Client.APP_IOS),
	/** 米花支付 */
	MhPay(14, "MH", true, false, false, Client.WAP, Client.APP_ANDROID, Client.APP_IOS),

	;

	// 定义私有变量
	public final Integer value;
	/** 订单号前缀 */
	public final String prefix;
	/** 支持充值 */
	public final boolean recharge;
	/** 支持提现 */
	public final boolean cash;
	/** 是否支持在线提现 */
	public final boolean liveCash;
	/** 支持的客户端标识 */
	public final int clientFlag;
	/** 默认的服务标识 */
	public String serviceId;

	final ValueProxy<PayPlatform, Integer> proxy;

	// 构造函数，枚举类型只能为私有
	PayPlatform(Integer value, String rechargePrefix, boolean recharge, boolean cash, boolean liveCash, Client... supportedClients) {
		this.value = value;
		this.prefix = rechargePrefix;
		this.recharge = recharge;
		this.cash = cash;
		this.liveCash = liveCash;
		int flag = 0;
		for (Client c : supportedClients) {
			flag |= 1 << c.ordinal();
		}
		this.clientFlag = flag;
		this.proxy = new ValueProxy<>(this, value, deprecated() ? name() + "（废弃）" : name());
	}

	public static final PayPlatform[] CACHE = values();

	public static PayPlatform of(@Nullable Integer value) {
		return Common.getEnum(CACHE, PayPlatform::getValue, value);
	}

	public boolean supports(boolean rechargeOrCash, @Nonnull Client client) {
		if (rechargeOrCash ? recharge : cash) {
			return BizFlag.hasFlag(clientFlag, 1 << client.ordinal());
		}
		return false;
	}

	/** 已被废弃 */
	public boolean deprecated() {
		// 在构造方法中执行，不能使用实例比较
		return ordinal() == 4 || ordinal() == 5;
	}

}
